home *** CD-ROM | disk | FTP | other *** search
/ C & C++ Multimedia Cyber Classroom / C and C++ Multimedia Cyber Classroom (Prentice Hall) (1998).iso / src / fig23_12.jar / Ch23 / Fig23_12 / Fig23_12.c < prev   
C/C++ Source or Header  |  1993-11-09  |  970b  |  40 lines

  1. /* Writing to a random access file */
  2. #include <stdio.h>
  3.  
  4. struct clientData {
  5.    int acctNum;
  6.    char lastName[15];
  7.    char firstName[10];
  8.    float balance;
  9. };
  10.  
  11. main()
  12. {
  13.    FILE *cfPtr;
  14.    struct clientData client;
  15.  
  16.    if ((cfPtr = fopen("credit.dat", "r+")) == NULL)
  17.       printf("File could not be opened.\n");
  18.    else {
  19.       printf("Enter account number"
  20.              " (1 to 100, 0 to end input)\n? ");
  21.       scanf("%d", &client.acctNum);
  22.  
  23.       while (client.acctNum != 0) {
  24.          printf("Enter lastname, firstname, balance\n? ");
  25.          scanf("%s%s%f", &client.lastName, 
  26.                &client.firstName, &client.balance);
  27.          fseek(cfPtr, (client.acctNum - 1) * 
  28.                sizeof(struct clientData), SEEK_SET);
  29.          fwrite(&client, sizeof(struct clientData), 1, cfPtr);
  30.          printf("Enter account number\n? ");
  31.          scanf("%d", &client.acctNum);
  32.       }
  33.    }
  34.  
  35.    fclose(cfPtr);
  36.  
  37.    return 0;
  38. }
  39.  
  40.